﻿using blackhouse.WebNemo.Business.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace blackhouse.WebNemo.Business.Groups {
    public class ViewGroup : Group, IGroup {

        #region Fields

        private IGroupOperations operations;

        #endregion

        #region Constructors

        public ViewGroup()
            : base(0) {
            this.operations = Factory.Instance().GetGroupOperators();
            this.decoGroup = new ImpGroup();
            (this.decoGroup as ImpGroup).Type = GroupType.View;
        }

        #endregion

        #region Methods

        public void SaveNew(string systemName, string selectDeclaration) {
            systemName = systemName.ToUpper();
            if (this.operations.CheckName(systemName))
                throw new CreateGroupException();
            if (!this.operations.CheckViewDeclaration(selectDeclaration))
                throw new CreateGroupException(4001);
            int groupId = this.operations.CreateView(systemName, selectDeclaration);
            if (groupId > 0) {
                IGroup g = Factory.Instance().GetGroup(groupId);
                GroupCopier copy = new GroupCopier(this.decoGroup, g);
                copy.Copy();
                this.decoGroup = g;
                base.Save();
            }
        }

        #endregion

    }
}
